package com.example.demo.controller;

import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created with LXD
 *
 * @author:Luhui
 * @qq:729913162
 * @Date:2018-07-09
 * @Time:16:10
 */
@RestController
public class HelloController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    private static final String REST_URL_PREFIX="http://PROVIDER";
//    private static final String REST_URL_PREFIX="http://127.0.0.1:8080";

    @GetMapping("/hello")
    public User hello(int id){
        return restTemplate.getForObject(REST_URL_PREFIX +"/find/"+id,User.class);
    }

    @GetMapping("/client")
    public String discovery(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/discovery",String.class);
    }

    @GetMapping("/discovery")
    public Object discoveryClient(){
        List<String> list = discoveryClient.getServices();
        System.out.println("*************"+list);
        List<ServiceInstance> instances = discoveryClient.getInstances("provider-user");
        System.out.println("===========================================");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()
                    +"\t"+instance.getUri());
        }
        return this.discoveryClient;
    }
}
